package com.lottery.ruok.lottrey.utils;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

import static okhttp3.internal.http.HttpDate.format;

/*
 *create by lm   on  2018/8/1
 *describe:时间选择器起始以及结束的年月日
 */
public class TimeUtil {
    public static final  SimpleDateFormat MONTH_DAY_DATE_FORMAT = new SimpleDateFormat("MM-dd");
    public static final  SimpleDateFormat HOUR_MINUTE_DATE_FORMAT = new SimpleDateFormat("HH:mm");
    public final static String FORMAT_BASIC_DATE_TIME = "yyyy-MM-dd HH:mm";
    public final static String FORMAT_ACTIVE_TIME = "MM/dd";

    public TimeUtil() {

    }

    public static String getActiveTime(String time) {
        SimpleDateFormat sdf1 = new SimpleDateFormat(FORMAT_ACTIVE_TIME);
        SimpleDateFormat sdf2 = new SimpleDateFormat(FORMAT_BASIC_DATE_TIME);
        String res = time;
        try {
            res = sdf1.format(sdf2.parse(time));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return res;
    }

    public static String getEndTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        String startTime = sdf.format(new Date());


        String endTime = startTime.substring(0, startTime.length() - 2);
        endTime = endTime + "01";
        return endTime;
    }

    public static String getStartTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        //String startTime = sdf.format(new Date());
        return sdf.format(new Date());
    }

    @SuppressLint("SimpleDateFormat")
    public static String getDayFormatTime(SimpleDateFormat simpleDateFormat,String timeStamp) {
        long time = Long.valueOf(timeStamp);
        int i = Integer.parseInt(String.valueOf(time));
        return simpleDateFormat.format(new Date(i * 1000L));
    }
    //时间戳转换日期
    //本文来自 meixi_android 的CSDN 博客 ，全文地址请点击：https://blog.csdn.net/meixi_android/article/details/72731493?utm_source=copy
    public static String timetodate(String time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(Long.valueOf(time));
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");//这里的格式可换"yyyy年-MM月dd日-HH时mm分ss秒"等等格式

        String date = sf.format(calendar.getTime());
        return date;
    }

    /*
     * 将时间转换为时间戳
     */
    public static String dateToStamp(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }
    /**
     * 当前时间所在一周的周一和周日时间
     * @param
     * @return   https://blog.csdn.net/Blue_Red_1314/article/details/78601727?utm_source=copy
     */
    public static Map<String,String> getWeekDate() {
        Map<String,String> map = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if(dayWeek==1){
            dayWeek = 8;
        }
        //  System.out.println("要计算日期为:" + sdf.format(cal.getTime())+ DateUtils.getWeek()); // 输出要计算日期

        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        Date mondayDate = cal.getTime();
        String weekBegin = sdf.format(mondayDate);
        System.out.println("所在周星期一的日期：" + weekBegin);


        cal.add(Calendar.DATE, 4 +cal.getFirstDayOfWeek());
        Date sundayDate = cal.getTime();
        String weekEnd = sdf.format(sundayDate);
        System.out.println("所在周星期日的日期：" + weekEnd);

        map.put("mondayDate", weekBegin);
        map.put("sundayDate", weekEnd);
        return map;
    }
    public static Map<String,String> getLastWeekDate() {
        Map<String,String> map = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if(dayWeek==1){
            dayWeek = 15;
        }
        //  System.out.println("要计算日期为:" + sdf.format(cal.getTime())+ DateUtils.getWeek()); // 输出要计算日期

        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        Date mondayDate = cal.getTime();
        String weekBegin = sdf.format(mondayDate);
        System.out.println("所在周星期一的日期：" + weekBegin);


        cal.add(Calendar.DATE, 4 +cal.getFirstDayOfWeek());
        Date sundayDate = cal.getTime();
        String weekEnd = sdf.format(sundayDate);
        System.out.println("所在周星期日的日期：" + weekEnd);

        map.put("mondayDate", weekBegin);
        map.put("sundayDate", weekEnd);
        return map;
    }

    /**
     * 将开奖时间转成时分秒
     * @param createTime
     * @return
     */
    public static String parseCreateTime(String createTime){
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        if(TextUtils.isEmpty(createTime)){
            Date date = new Date();
            return sdf.format(date);
        }else{
            if(createTime.length()>8){
                return createTime.substring(createTime.length()-8);
            }
        }
        return "";
    }

}
